package com.rabbiter.oes.controller;

import com.rabbiter.oes.entity.FillQuestion;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.FillQuestionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "填空题")
@RestController
public class FillQuestionController {

    @Autowired
    private FillQuestionServiceImpl fillQuestionService;

    @ApiOperation(value = "添加填空题", notes = "添加填空题", httpMethod = "POST")
    @PostMapping("/fillQuestion")
    public R add(@RequestBody FillQuestion fillQuestion) {
        fillQuestion.setQuestion(fillQuestion.getQuestion().replaceAll("（）", "()"));
        Integer questionId = fillQuestionService.add(fillQuestion);
        if (questionId != 0) {
            return R.ok("添加成功").data(questionId);
        }
        return R.logicError("添加失败").data(questionId);
    }

    @ApiOperation(value = "查询最后一条填空题", notes = "查询最后一条填空题", httpMethod = "GET")
    @GetMapping("/fillQuestionId")
    public R findOnlyQuestionId() {
        FillQuestion res = fillQuestionService.findOnlyQuestionId();
        return R.ok("查询成功").data(res);
    }

    @ApiOperation(value = "修改填空题", notes = "修改填空题", httpMethod = "POST")
    @PostMapping("/editFillQuestion")
    public R edit(@RequestBody FillQuestion fillQuestion) {
        fillQuestion.setQuestion(fillQuestion.getQuestion().replaceAll("（）", "()"));
        int res = fillQuestionService.edit(fillQuestion);
        if (res != 0) {
            return R.ok("修改成功").data(res);
        }
        return R.logicError("修改失败").data(res);
    }
}
